﻿using System;
using System.Configuration;
using System.Web.UI;
using com.huigo.Framework.Passport;

namespace com.huigo.B2B.Web
{
    public class BasePage : System.Web.UI.Page
    {
        #region 窗体加载
        protected override void OnLoad(EventArgs e)
        {
            //#if !DEBUG
            // 判断用户登录
            if (!CustPassport.ChkCookieLogin())
            {
                string url = string.Empty;
                try
                {
                    url = Request.Url.AbsoluteUri;
                    url = url.Replace("&", "{$}");
                }
                catch { }

                if (url == string.Empty)
                    Response.Redirect("/Login.aspx");
                else
                    Response.Redirect(string.Format("/Login.aspx?toUrl={0}", com.loto.Common.Utils.UrlEncode(url)));
            }
            //#endif
            base.OnLoad(e);
        }
        #endregion

        protected string CustName
        {
            get
            {
                //#if DEBUG
                //                return "admin";
                //#else
                //return UserPassport.GetCookieUsername(UserPassport.EncodingType.UTF8);
                return CustPassport.GetCookieUserName();
                //#endif
            }
        }

        protected long CustId
        {
            get
            {
                //#if DEBUG
                //                return new Guid("3A8A7052-BB3B-42F0-B55A-508C7AFF9135");
                //#else
                return CustPassport.GetCookieUserId();
                //#endif
            }
        }

        protected string CName
        {
            get
            {
                //#if DEBUG
                //                return new Guid("3A8A7052-BB3B-42F0-B55A-508C7AFF9135");
                //#else
                return CustPassport.GetCookieUName();
                //#endif
            }
        }

        public decimal ConvertCurrencyPay(decimal currency)
        {
            /*舍分处理
            float f = (float)System.Math.Floor(currency);
            float d = currency - f;
            if (d.ToString().Length > 2)
            {
                return f+float.Parse(d.ToString().Substring(0, 3));
            }
            return currency;
            */
            decimal concurrency = (decimal)Math.Round(currency, 2);
            decimal thridCurrency = (decimal)Math.Round(currency, 3);
            if (concurrency - thridCurrency < 0)
            {
                concurrency = concurrency + 0.01M;
            }
            return (decimal)Math.Round(concurrency, 2);
        }

        public string RequestString(string name)
        {
            string v = Request[name];
            if (v == null)
            {
                v = string.Empty;
            }
            v = v.Replace("<", "&lt;");
            v = v.Replace(">", "&gt;");
            return v.Trim();
        }
        public decimal RequestDecimal(string name)
        {
            string v = Request[name];
            if (v == null)
            {
                return 0;
            }
            else
            {
                try
                {
                    return Convert.ToDecimal(v);
                }
                catch
                {
                    return 0;
                }
            }
        }
        public void PageRedirect()
        {
            string url = Request.Url.AbsoluteUri;
            url = url.Replace("&", "{$}");
            Response.Redirect(string.Format("/err.aspx?url={0}", com.loto.Common.Utils.UrlEncode(url)));
        }
    }
}
